'''
Created on 2.17, 2016
logregression_gradient descent

Input:      inX: vector to compare to existing dataset (100x3)
            regressiondata.text
Output:     the most popular class label(0/1)

@author: XFBY
'''
import time
import numpy as np
import matplotlib.pyplot as plt
import os
import time
pass
def LoadData(data):
    xset = []
    yset = []
    file = open(data,'r')
    for line in file:
        data = line.strip().split()
        xset.append([1.0,float(data[0]),float(data[1])])
        yset.append(float(data[2]))
    return np.mat(xset),np.mat(yset).transpose()
def TrainSet(maxnum,alpha,xset,yset):
    theta = np.ones((3,1))
    cost = []
    i = 0
    while(i < maxnum):
        hx = xset * theta
        erro =hx - yset
        loss = np.sum(erro)
        cost.append(loss)
        gradient = xset.transpose()*erro
        theta = theta - alpha * gradient
        i+= 1
        pass
    return theta,cost
def draw(xset,yset,weights):
    xnline,xnrow = xset.shape
    print(xnline,xnrow)
    for i in range(xnline):
        if yset[i,0] == 0:
            plt.plot(xset[i,1],xset[i,2],'or')
        if yset[i,0] == 1:
            plt.plot(xset[i,1],xset[i,2],'ob')
            pass
    #plt.show()
    #draw the classify line
    weight = weights.getA()
    minx = min(xset[:,1])[0,0]
    maxx = max(xset[:,1])[0,0]
    print(minx,maxx,weight)
    
    yminx = float(-weight[0]-weight[1]*minx)/weight[2]
    ymaxx = float(-weight[0]-weight[1]*maxx)/weight[2]
    plt.plot([minx,maxx],[yminx,ymaxx],'-g')
    
    plt.show()
    
xset,yset = LoadData("regressiondata.text")
theta1,cost = TrainSet(1500, 0.000001, xset, yset)
#print(theta1,'\n',cost)
cx = range(len(cost))
plt.plot(cx,cost)
plt.ylim(-40,700)
plt.xlabel('trainsnum');plt.ylabel('cost')
plt.show()
draw(xset, yset, theta1)
